package cn.dhnep.demo.controller;


import cn.dhnep.demo.pojo.Horseman;
import cn.dhnep.demo.pojo.Merchant;
import cn.dhnep.demo.service.MerchantService;
import cn.dhnep.demo.util.Page;
import cn.dhnep.demo.util.ResultMap;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  商家端
 * </p>
 *
 * @author dhn
 * @since 2021-09-06
 */
@RestController
@RequestMapping("merchant")
public class MerchantController {

    @Resource
    private MerchantService merchantService;

    private Map<String,Object> result = new HashMap<>();


    @ApiOperation("商家分页查询")
    @RequestMapping("merchantlistpage")
    public ResultMap<List<Merchant>> MerchantList(Page page, @RequestParam("limit") Integer limit) throws UnsupportedEncodingException {
        page.setRows(limit);
        List<Merchant>merchantList=merchantService.selectPageList(page);
        int totals=merchantService.selectPageCount(page);
        page.setTotalRecord(totals);
        return new ResultMap<List<Merchant>>("",merchantList,0,totals);
    }

    @ApiOperation("新增商户")
    @RequestMapping("merchantAdd")
    public Map<String,Object> merchantAdd(@RequestBody Merchant merchant, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        if (merchant.getMerchantFid()==null){
            merchant.setMerchantFid(0);
        }
        int count =merchantService.merchantAdd(merchant);
        if (count==1){
            result.put("result","success");
        }else{
            result.put("result","error");
        }
        return result;
    }

}

